﻿using System;
using System.Configuration;
using System.Windows.Forms;

//自定义命名空间
using DreamCube.Apps.Common.WinForm.Controls;
using DreamCube.Apps.Common.WinForm.Controls.Progress;

namespace PicServer.UI
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            BindEvent();
        }

        #region "私有方法"

        /// <summary>
        /// 绑定事件
        /// </summary>
        private void BindEvent()
        {
            this.Load += new EventHandler(MainForm_Load);
            this.FormClosing += new FormClosingEventHandler(MainForm_FormClosing);
            this.btn_initialDB.Click += new EventHandler(btn_initialDB_Click);
            this.btn_startService.Click += new EventHandler(btn_startService_Click);
        }

        /// <summary>
        /// 关闭窗口前触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBoxHelper.YesNo(this, "确定退出服务并关闭服务程序吗？") == System.Windows.Forms.DialogResult.Yes)
                e.Cancel = false;
            else
                e.Cancel = true;
        }

        /// <summary>
        /// 启动服务的按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btn_startService_Click(object sender, EventArgs e)
        {
            ProgressFormStyle style = null;
            if (BLL.FileService.ServerState == Model.ServerState.Running)
            {
                style = new ProgressFormStyle()
                {
                    ShowCancleBtn = true,
                    TimeLength = 20,
                    LabelText = "正在停止服务",
                    FormTitle = "正在停止服务，请稍后...."
                };
                DialogResult result = ProgressBarHelper.ShowAutoProgressDialog(this, new Action(() =>
                {
                    Services.MainService.Stop();
                }), 60, style);
                if (result == System.Windows.Forms.DialogResult.OK)
                    btn_startService.Text = "启动服务";
            }
            else
            {
                style = new ProgressFormStyle()
                {
                    ShowCancleBtn = true,
                    TimeLength = 20,
                    LabelText = "正在启动服务",
                    FormTitle = "正在启动服务，请稍后...."
                };
                DialogResult result = ProgressBarHelper.ShowAutoProgressDialog(this, new Action(() =>
                {
                    Services.MainService.Start(
                            (Model.ServiceStartType)Enum.Parse(typeof(Model.ServiceStartType), ConfigurationManager.AppSettings["ServiceStartType"], true),
                            ConfigurationManager.AppSettings["FileFolderPath"],
                            Convert.ToInt64(ConfigurationManager.AppSettings["FileMaxSize"]),
                            Convert.ToInt32(ConfigurationManager.AppSettings["PerfileMaxSize"]),
                            Convert.ToInt32(ConfigurationManager.AppSettings["FileTableCount"]),
                            ConfigurationManager.AppSettings["PicServerWebUri"],
                            ConfigurationManager.AppSettings["PicServerUri"]);
                }), 60, style);
                if (result == System.Windows.Forms.DialogResult.OK)
                    btn_startService.Text = "停止服务";
            }
        }

        /// <summary>
        /// 初始化数据库的按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btn_initialDB_Click(object sender, EventArgs e)
        {
            ProgressFormStyle style = new ProgressFormStyle()
            {
                ShowCancleBtn = true,
                TimeLength = 20,
                LabelText = "正在初始化数据库",
                FormTitle = "正在初始化数据库，请稍后...."
            };
            DialogResult result = ProgressBarHelper.ShowAutoProgressDialog(this, new Action(() =>
                            {
                                DAL.DatabaseDetails.InitialDB(
                                   Int32.Parse(ConfigurationManager.AppSettings["FileTableCount"]));
                            }), 60, style);
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                MessageBoxHelper.Information(this, "初始化数据库执行成功");
                //再重复连接数据库
                ConnectDB();
            }
            else if (result == System.Windows.Forms.DialogResult.Abort)
            {
                MessageBoxHelper.Information(this, "初始化数据库执行超时");
            }
        }

        /// <summary>
        /// 窗体加载的时候触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MainForm_Load(object sender, EventArgs e)
        {
            Action a = new Action(() => { ConnectDB(); });
            a.BeginInvoke(null, null);
        }

        /// <summary>
        /// 连接数据库
        /// </summary>
        void ConnectDB()
        {
            ControlHelper.ControlBeginInvoke(this, new Action(() => {
                lb_connected.Text = DAL.DatabaseDetails.IsConnected ? "连接成功" : "连接失败";
                lb_createTime.Text = DAL.DatabaseDetails.CreateTime;
                lb_dbVersion.Text = DAL.DatabaseDetails.DbVersion;
                lb_hasInitial.Text = DAL.DatabaseDetails.HasInitial ? "已初始化" : "未初始化";
                btn_initialDB.Enabled = !DAL.DatabaseDetails.HasInitial;
                btn_startService.Enabled = DAL.DatabaseDetails.HasInitial;

                lb_connected.Visible = true;
                lb_createTime.Visible = true;
                lb_dbVersion.Visible = true;
                lb_hasInitial.Visible = true;
            }));
        }

        #endregion
    }
}
